package it.enricod.jcontextfree.engine.samples.impl;

import it.enricod.jcontextfree.engine.IProgramLoader;
import it.enricod.jcontextfree.engine.model.IProgram;
import it.enricod.jcontextfree.engine.samples.IProgramCatalogEntry;
import it.enricod.jcontextfree.engine.utils.IOUtils;

/**
 *
 * @author edonelli
 */
public class ProgramCatalogEntry implements IProgramCatalogEntry{
    private String id;
    private String path;
    private String programAsText = null;
    private IProgram program = null;
    private IProgramLoader programLoader;

    public ProgramCatalogEntry(String id, String path, IProgramLoader programLoader) {
        this.id = id;
        this.path = path;
        this.programLoader = programLoader;
    }
    public String getId() {
       return id;
    }

    public String getProgramAsText() {
        if (programAsText == null ) {
            programAsText = IOUtils.readFileAsString(path);
        }
        return programAsText;
    }

    public IProgram getProgram() {
        if (program == null ) {
            program = programLoader.load(getProgramAsText());
        }
        return program;
    }

}
